﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace UbbTimetable2
{
    /// <summary>
    /// A form for class data manipulation.
    /// </summary>
    public partial class ClassForm : Form
    {

        private MainForm _parent;
        private ClassesDao _classesDao;
        private int _selectedId;

        /// <summary>
        /// Main constructor.
        /// </summary>
        /// <param name="parent">The parent MainForm that will be frozen.</param>
        public ClassForm(MainForm parent)
        {
            InitializeComponent();

            _parent = parent;
            _classesDao = new ClassesDao();
            _parent.Enabled = false;
        }

        private void ClassForm_Load(object sender, EventArgs e)
        {
            LoadAll();
            SetInputStates(false);
            cbNew.Enabled = false;
        }

        /// <summary>
        /// Fills the form with data.
        /// </summary>
        private void LoadAll()
        {
            fillDgvView();

            // Fill the professor combos
            List<Prof> profList = _parent.ProfDal.GetProfList();
            _parent.LoadProfCb(profList, cbProfessor);

            List<ClassType> typeList = _classesDao.GetTypes();
            LoadClassTypesCb(typeList, cbType);

            List<ClassNames> nameList = _classesDao.GetClassNames();
            LoadClassNamesCb(nameList, cbName);
        }

        /// <summary>
        /// Fills the data grid view with data.
        /// </summary>
        private void fillDgvView()
        {
            Utilities.FillDgv(_classesDao, dgvClasses);
        }

        /// <summary>
        /// Fill a combo with a list of class types
        /// </summary>
        /// <param name="list">The list containing the data</param>
        /// <param name="cb">The combo to be filled</param>
        private void LoadClassTypesCb(List<ClassType> list, ComboBox cb)
        {
            cb.DataSource = list;
            cb.DisplayMember = "TypeName";
            cb.ValueMember = "TypeId";
        }

        /// <summary>
        /// Fill a combo with a list of class names
        /// </summary>
        /// <param name="list">The list containing the data</param>
        /// <param name="cb">The combo to be filled</param>
        private void LoadClassNamesCb(List<ClassNames> list, ComboBox cb)
        {
            cb.DataSource = list;
            cb.DisplayMember = "ClassName";
            cb.ValueMember = "ClassId";
        }

        private void btNew_Click(object sender, EventArgs e)
        {
            cbNew.Checked = true;
            SetInputStates(true);
        }

        private void btSave_Click(object sender, EventArgs e)
        {
            if (cbNew.Checked)
            {
                _classesDao.InsertClass(Convert.ToInt32(cbProfessor.SelectedValue),
                    Convert.ToInt32(cbName.SelectedValue), Convert.ToInt32(cbType.SelectedValue));
            }
            else
            {
                _classesDao.UpdateClass(_selectedId, Convert.ToInt32(cbProfessor.SelectedValue),
                    Convert.ToInt32(cbName.SelectedValue), Convert.ToInt32(cbType.SelectedValue));
                if (_classesDao.Err != "OK")
                {
                    MessageBox.Show("Ez a tanar nem tudja elvallalni a mar beutemezett orakat!");
                }
            }
            cbNew.Checked = false;
            SetInputStates(false);
            fillDgvView();
        }

        /// <summary>
        /// Sets the input widgets state.
        /// </summary>
        /// <param name="state">The new state.</param>
        private void SetInputStates(Boolean state)
        {
            cbName.Enabled = state;
            cbType.Enabled = state;
            cbProfessor.Enabled = state;
            btSave.Enabled = state;
            btCancel.Enabled = state;
            dgvClasses.Enabled = !state;
        }

        private void btCancel_Click(object sender, EventArgs e)
        {
            cbNew.Checked = false;
            SetInputStates(false);
        }

        private void btUpdate_Click(object sender, EventArgs e)
        {
            if (dgvClasses.SelectedRows.Count == 1)
            {
                cbNew.Checked = false;

                cbType.Text = dgvClasses.SelectedRows[0].Cells[1].Value.ToString();
                cbName.Text = dgvClasses.SelectedRows[0].Cells[2].Value.ToString();
                cbProfessor.Text = dgvClasses.SelectedRows[0].Cells[3].Value.ToString();
                _selectedId = Convert.ToInt32(dgvClasses.SelectedRows[0].Cells[0].Value);

                SetInputStates(true);
            }
        }

        private void btDelete_Click(object sender, EventArgs e)
        {
            if (dgvClasses.SelectedRows.Count == 1)
            {
                _selectedId = Convert.ToInt32(dgvClasses.SelectedRows[0].Cells[0].Value);
                DialogResult dlg = MessageBox.Show("Delete item?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (dlg == DialogResult.Yes)
                {
                    _classesDao.DeleteClass(_selectedId);
                }
                fillDgvView();
            }
        }

        private void ClassForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            _parent.Enabled = true;
        }
    }
}
